// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_letter = require("./inquireLogisticsInfoDetail1.css");
	var databind = require("databind");
	var tools = require("tools");
	var tplDetail = '{@each datas as it}' +
		'				{@each it.resultList as item,index}' +
		'				{@if index === 0}' +
		'					<div><img src="../../../res/png/point_ing.png" />${item.traceDateTime}<br /><span>【最新】&nbsp;&nbsp;&nbsp;&nbsp;${item.desc}</span></div>' +
		'				{@else}' +
		'					<div><img src="../../../res/png/point_ed.png" />${item.traceDateTime}<br /><span>&nbsp;&nbsp;&nbsp;&nbsp;${item.desc}</span></div>' +
		'				{@/if}' +
		'				{@/each}' +
		'				{@/each}';
	var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var init = function(page) {
		var oData = {
			pageIndex: pageIndex,
			pageSize: pageSize,
			totalPageCount: totalPageCount,
			totalRecordCount: totalRecordCount
		};
		if (page.params != null) {
			var inventoryId = page.params.inventoryId;
			oData.inventoryId = inventoryId;
			tools.loadList("inquireBulkstockInventoryPageList.json", $("#logisticsDetailBody"), $("#logisticsDetailTpl"), oData, function() {
				$.each($("#inquireLogisticsInfoDetail1 .detail"), function() {
					var $this = $(this);
					var attr = $this.parents("dd").find(".span4 a img").attr("src");
					$this.html('加载物流信息中<img src="../../../res/png/loading.gif"/>');
					inquireFinanceContainerInventoryTrackingDetail($.trim($this.parents("dd").find(".span1").html()), $.trim($this.parents("dd").attr("waybillNumber")), $this.parents("dd").attr("financeNumber"), $this);
					$this.parents("dd").find(".order_detail").removeClass("none");
				});
				
				$("#inquireLogisticsInfoDetail1").on("click", ".detail", function() {
					var $this = $(this);
					var attr = $this.parents("dd").find(".span4 a img").attr("src");
					if (attr == "../../../res/png/arrow8_icon.png") {
						$this.parents("dd").find(".order_detail").addClass("none");
						$this.html('展开物流信息中<img src="../../../res/png/arrow1_icon.png"/>');
					} else {
						$this.html('加载物流信息中<img src="../../../res/png/loading.gif"/>');
						inquireFinanceContainerInventoryTrackingDetail($.trim($this.parents("dd").find(".span1").html()), $.trim($this.parents("dd").attr("waybillNumber")), $this.parents("dd").attr("financeNumber"), $this);
						$this.parents("dd").find(".order_detail").removeClass("none");
					}
				});
			});
		} else {
			tools.loadList("inquireBulkstockInventoryPageList.json", $("#logisticsDetailBody"), $("#logisticsDetailTpl"), oData, function() {
				$("#inquireLogisticsInfoDetail1").on("click", ".detail", function() {
					var $this = $(this);
					var attr = $this.parents("dd").find(".span4 a img").attr("src");
					if (attr == "../../../res/png/arrow8_icon.png") {
						$this.parents("dd").find(".order_detail").addClass("none");
						$this.parents("dd").find(".span4 a img").attr("src", "../../../res/png/arrow1_icon.png");
					} else {
						$this.parents("dd").find(".span4 a img").attr("src", "../../../res/png/arrow8_icon.png");
						inquireContainerInventoryTrackingDetail($this);
						$this.parents("dd").find(".order_detail").removeClass("none");
					}
				});
			});
		}
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}
	};

	//查询融资箱货物流明细
	var inquireContainerInventoryTrackingDetail = function(dom) {
		base.fnAjax({
			action: "inquireContainerInventoryTrackingDetail.json",
			async: false,
			oData: {
				inventoryId: $(dom).parents("dd").attr("inventoryId")
					//				inventoryId: 102
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = tplDetail;
					var html = juicer(tpl, data);
					$(dom).parents("dd").find(".order_detail").html(html);
				} else {}
			}
		});
	};

	//查询融资箱货物流明细
	var inquireFinanceContainerInventoryTrackingDetail = function(caseNumber, waybillNumber, financeNumber, dom) {
		base.fnAjax({
			action: "inquireFinanceContainerInventoryTrackingDetail.json",
			async: false,
			oData: {
				caseNumber: caseNumber,
				waybillNumber: waybillNumber
//				waybillNumber: "ATHKTJ15517260",
//				caseNumber: "ATLU0003228"
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = tplDetail;
					var html = juicer(tpl, data);
					$(dom).parents("dd").find(".order_detail").html(html);
					$(dom).parents("dd").find(".detail").html('收起物流追踪详情<img src="../../../res/png/arrow8_icon.png"/>');
				} else {
					base.alert(3, "暂无该箱物流信息");
				}
			}
		});
	};

	return {
		init: init
	};
});